<template>
  <div>
    <h4>系统编码: </h4>
    <div>{{ systemCode }}</div>
    <br />
    <div>
      <a-textarea v-model:value="licenseInfo.license" :rows="4" style="width: 100%" />
    </div>
    <br />
    <a-button type="primary" @click="save()">{{ l('Save') }}</a-button>
  </div>
</template>

<script lang="ts">
  import { defineComponent } from 'vue';
  import { AppComponentBase } from '/@/shared';
  import { ProjectLicense, SharedSessionServiceProxy } from '/@/shared/service-proxies';

  export default defineComponent({
    name: 'set-license',
    mixins: [AppComponentBase],
    props: {
      displayTips: {
        type: Boolean,
        default: true,
      },
      license: {
        type: String,
        default: '',
      },
    },
    data() {
      return {
        sharedSessionSer: new SharedSessionServiceProxy(),
        tips: '',
        alertType: 'error',
        systemCode: '',
        licenseInfo: new ProjectLicense(),
      };
    },
    created() {
      this.fillServiceProxy();

      this.systemCode = this.abp.setting.get('SystemCode');
      this.licenseInfo = new ProjectLicense(JSON.parse(this.abp.setting.get('License')));
      if (!this.licenseInfo.isExist) {
        // 不存在授权
        this.tips = this.l('LicenseNotExist');
      } else if (this.licenseInfo.isExpires) {
        // 授权过期
        this.tips = this.l('LicenseExpired');
      } else {
        this.alertType = 'warning';
        this.tips = this.l('LicenseExist');
      }
    },

    methods: {
      gotoHome() {
        if (location.href.includes('/license/view')) {
          location.reload();
        } else {
          location.href = '/';
        }
      },
      save() {
        this.loading = true;
        this.sharedSessionSer
          .updateLicense(this.licenseInfo.license)
          .finally(() => {
            this.loading = false;
          })
          .then((res) => {
            // 校验失败
            if (!res) {
              this.abp.notify.error(this.l('InvalidLicense'));
              return;
            }

            this.abp.notify.success(this.l('ValidLicense'));

            setTimeout(() => {
              this.gotoHome();
            }, 3000);
          });
      },
    },
  });
</script>
